<?php
include_once 'BaseS.php';
include_once $_SERVER ['DOCUMENT_ROOT'].BASE_PROJECT_PATH.'/application/model/UserM.php';
include_once $_SERVER ['DOCUMENT_ROOT'].BASE_PROJECT_PATH.'/library/util/Util.php';

class UserS extends BaseS {
	
	public function __construct() {
		parent::__construct ();
		parent::setTarget ( $this );
	}

	public function insert() {
		
		$result = array("sl"=>"1", "di"=>"0", "ti"=>"新增結果", "msg"=>"新增失敗！"	);
		$obj = $this->getRequest("datas");
		$dao = new UserM();
		$obj['date_create'] = Util::getNowDate("Y-m-d H:i:s");
		
		if(isset($obj['password'])){$obj['ps_e'] = Util::encrypt($obj['password']);}
		if($dao->insert($obj)){$result['msg'] = "新增成功！";}
		
		$this->logUserAction(Util::clientIp(), "管理者", 1);
		echo $result; 
	}
	
	public function update(){
		
		$result = array("sl"=>"1", "di"=>"1","ti"=>"修改結果", "msg"=>"修改失敗!");
		$obj = $this->getRequest("datas");
		$dao = new UserM();
		
		if(isset($obj['password'])){$obj['ps_e'] = Util::encrypt($obj['password']);}
		if($dao->update($obj)){$result['msg'] = "修改成功！";}
		
		$this->logUserAction(Util::clientIp(), "管理者", 2);
		
		if($_SESSION["user"]->id == $obj['id']){
			$_SESSION["user"] = $dao->getUser($_SESSION["user"]->account, $obj['ps_e']);
		}
		
		echo Util::toJson($result);
	}
	
	public function delete(){
		
		$result = array("sl"=>"1", "di"=>"1","ti"=>"刪除結果", "msg"=>"刪除失敗!");
		$id = $this->getRequest("id");
		$dao = new UserM();
		if($dao->delete($id)){$result['msg'] = "刪除成功！";}
		$this->logUserAction(Util::clientIp(), "管理者", 3);
		echo $result;
	}
	
	public function getTableData(){
		
		$result = array(
				"sl"    => "1",
				"di" 	=> "0",
				"ti" 	=> "",
				"msg" 	=> ""
		);
		
		$key = $this->getRequest("dataId");
		$dao = new UserM();
		$result['data'] = $dao->getTableData($key);
		
		if($result["data"] == null){
			$result["di"] = "1";
			$result["ti"] = "查無資料";
			$result["msg"] = "資料庫沒有相關資料！";
		}
		
		echo Util::toJson($result);
	}
	
	public function queryTable(){
		 
		$result = array("sl"=>"1", "di"=>"0", "ti"=>"", "msg"=>"");
		$dao = new UserM();
		$obj = $this->getRequest("datas");
		$startPage = ($obj['page']-1) * $obj['pageRow']; 
		$searchStr = "";
		$result["data"] = $dao->queryTable($obj['orderData'], $startPage, $obj['pageRow'], $searchStr);
		
		if($result["data"] == null){
			$result["di"] = "1";
			$result["ti"] = "系統異常";
			$result["msg"] = "資料庫無法連接！";
		}
		
		if($result["data"]['count'] == 0){
			$result["di"] = "1";
			$result["ti"] = "查無資料";
			$result["msg"] = "資料庫沒有相關資料！";
		}
		
		echo Util::toJson($result); 
	}

	/**
	 * 後台系統登入
	 */
	public function login() {
		
	    $account =  $this->getRequest("account");	
	    //$password = $this->getRequest("password");
    	$password = Util::encrypt($this->getRequest("password"));
   
    	$dao = new UserM();
        $user = $dao->getUser($account, $password);	
    	
        if ($user == null) {
        	
        	$dao->plusLoginFail($account);
        	$dao->insertLogUserLoginout(array('user_account' => $account, 'ip' => Util::clientIp(), 'type' => 1, "is_login" => 0));
        	echo 2;
        	exit;
        	
        } else {
        	
        	$_SESSION["user"] = $user;
        	$liveKey = microtime(true) * 10000;
        	
        	$datas = array(
        			"id" => $user->id,
        			"live_key" => $liveKey,
        			"fail_times" => 0
        	);
        	
        	$user->live_key = $liveKey;
        	$dao->updateLiveKey($datas);
        	$_SESSION["user"] = $user;
        	
        	$dao->insertLogUserLoginout(array('user_account' => $user->account, 'ip' => Util::clientIp(), 'type' => 1));
        	
        	echo 1;
        	exit;
        }
	}
	
	/**
	 * 後台系統登出
	 */
	public function logout() {
		
		$dao = new UserM();
		$user = $_SESSION["user"] ;
        $dao->insertLogUserLoginout(array('user_account' => $user->account, 'ip' => Util::clientIp(), 'type' => 2));
		unset($_SESSION["user"]);
		Util::forward(BASE_PROJECT_PATH."/view/admin/login.php");
	}
}

if (isset($_GET ["action"])) {
	$service = new UserS ();
	$service->__call ( $_GET ["action"], null );
} 
?>